/*
 * Copyright (c) 2020. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.gehj.androidlibs.Utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import android.widget.Toast;

import androidx.core.app.NotificationManagerCompat;
import androidx.fragment.app.FragmentActivity;

import com.permissionx.guolindev.PermissionX;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import okhttp3.Response;

/**
  *
  * @ClassName:      FuctionUtils
  * @Description:    临时功能类，需要网allUtils进行总结
  * @Author:         gehj
  * @CreateDate:     2020/12/15 9:26
 */

public class FuctionUtils {

    /**
     * 401 == token失效
     * 返回首页
     *
     * */
    public  static void BackLogin(Response response, Context context,Class clz) {
        if (null != response) {
            if (response.code() == 401) {
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, "登录超时,重新登录", Toast.LENGTH_SHORT).show();
                    }
                });
                if (null!=context) {
                    Intent intent = new Intent(context, clz);
                    context.startActivity(intent);
                    if (Activity.class.isInstance(context) ) {
                        ((Activity)context).finish();
                    }
                }

            }/*else{
                new Handler(Looper.getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, response.message(), Toast.LENGTH_SHORT).show();
                    }
                });

            }*/
        }
    }

    public static  void getPermission(Context context ,String permisson){
        //单一权限添加
        PermissionX.init((FragmentActivity) context)
                .permissions(permisson
                )
                .setDialogTintColor(Color.parseColor("#008577"), Color.parseColor("#83e8dd"))
                .onExplainRequestReason((scope, deniedList) -> {
                    String message = "需要您同意以下权限才能正常使用";
                    scope.showRequestReasonDialog(deniedList, message, "确定", "取消");
                })
                .request((allGranted, grantedList, deniedList) -> {
                    if (allGranted) {
                        /*new Handler(Looper.getMainLooper()).post(
                                new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(context, "所有申请的权限都已通过", Toast.LENGTH_SHORT).show();

                                    }
                                }
                        );*/
                    } else {
                        // Toast.makeText(context, "您拒绝了如下权限："+deniedList, Toast.LENGTH_SHORT).show();
                         showMissingPermissionDialog((Activity)context);

                    }
                });




    }





    /**
     * 显示提示信息
     *
     * @since 2.5.0
     *
     */
    private static void showMissingPermissionDialog(Activity activity) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setTitle("提示");
        builder.setMessage("当前应用缺少必要权限。\\n\\n请点击\\\"设置\\\"-\\\"权限\\\"-打开所需权限。");

        // 拒绝, 退出应用
        builder.setNegativeButton("取消",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        activity.finish();
                    }
                });

        builder.setPositiveButton("设置",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //startAppSettings(activity);
                    }
                });

        builder.setCancelable(false);

        builder.show();
    }
















}
